/** @format */

import { resultsModel } from '../../sdk'
import { CODE_TYPES } from '../../errorCode'
import { cloundAlarm } from './dingTalk'
import { TRACK_EVENTS, APP_KEY } from '../../constants'
import dayjs from 'dayjs'

/** @format */

export async function doHandler(target: Object, method: IFunction, args: any[]) {
  let ret: IResult<any>
  try {
    ret = await method.apply(target, args)
  } catch (error) {
    console.log('error', error)
    const errMessage = error instanceof Error ? error.toString() : JSON.stringify(error)
    // 云告警
    cloundAlarm(args[0], errMessage)
    ret = resultsModel.error(CODE_TYPES.SYSTEM_ERROR)
  }
  if (!(ret as IErrorResult).success) {
    Object.assign(ret, {
      errorCode: (ret as IErrorResult)?.code,
      errorMessage: (ret as IErrorResult)?.message
    })
  }

  return ret
}
